/*
 * @Author: Turump 
 * @Date: 2021-03-31 09:18:09 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-04-21 14:30:11
 * 此组件用于封装一些基础功能组件的启用
 */

import { TaskBase } from "./TaskBase";

const { ccclass, property, menu, executionOrder } = cc._decorator;

@ccclass
@executionOrder(1)
@menu("任务组件/脚本启(禁)用代理")
export default class EnableComponentTask extends TaskBase {

    @property({ type: [cc.String], displayName: "脚本名", tooltip:"不支持多个同名脚本"})
    compNameArr: string[] = []

    @property({ displayName: "这是禁用任务" })
    isDisableTask = false;

    @property({ displayName: "任务完成Emit事件", tooltip: "任务完成Emit事件", override: true, visible: false })
    doneEvent = ""

    @property({ displayName: "任务支持暂停", override: true, visible: false })
    isPauseable = false;


    private compArr: cc.Component[] = []

    //V1.0是用接口方法来启动，注释之后，直接用脚本的Enable和Disable控制
    // enableMethodName = 'enableThis';
    // disableMethodName = 'disableThis'

    onLoad() {
        super.onLoad();
        this.compNameArr.forEach((compName: string) => {
            let comp = this.node.getComponent(compName)
            if (comp != null) {
                this.compArr.push(this.node.getComponent(compName))
            }
        })
    }

    start() {
        cc.log("EnableComponentTask start length:", this.compArr.length)
        super.start();
        if (this.isDisableTask == false) {
            this.compArr.forEach((comp: cc.Component) => {
                comp.enabled = false;
            })
        }
    }

    enableTask() {
        cc.log("EnableComponentTask enableTask length:", this.compArr.length)
        super.enableTask();
        this.compArr.forEach((comp: cc.Component) => {
            if (this.isDisableTask)
                comp.enabled = false;
            else
                comp.enabled = true;
        })
    }
}
